package com.zh.juc.completablefuture;

import java.util.concurrent.*;

/**
 * @Author: zhanghuan
 * @date: 2022/10/6 19:58
 * @description: create it
 */
public class CompletableFutureUseDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        try {
            CompletableFuture.supplyAsync(() -> {
                System.out.println(Thread.currentThread().getName() + "\t ----come in");
                int result = ThreadLocalRandom.current().nextInt(10);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("----------->1秒钟后出结果:" + result);
                if (result > 2) {
                    int i = 10 / 0;
                }
                return result;
            }, threadPool).whenComplete((v, e) -> {
                if (e == null) {
                    System.out.println("-----计算完成，更新UpdateVa:" + v);
                }
            }).exceptionally(e -> {
                e.printStackTrace();
                System.out.println("异常情况：" + e.getCause() + "\t" + e.getMessage());
                return null;
            });
            System.out.println(Thread.currentThread().getName() + "线程先去忙其他任务");
        } catch (Exception e) {

        } finally {
            threadPool.shutdown();
        }
//        CompletableFuture.supplyAsync(() -> {
//            System.out.println(Thread.currentThread().getName() + "\t ----come in");
//            int result = ThreadLocalRandom.current().nextInt(10);
//            try {
//                TimeUnit.SECONDS.sleep(1);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("----------->1秒钟后出结果:" + result);
//            return result;
//        }).whenComplete((v, e) -> {
//            if (e == null) {
//                System.out.println("-----计算完成，更新UpdateVa:" + v);
//            }
//        }).exceptionally(e -> {
//            e.printStackTrace();
//            System.out.println("异常情况：" + e.getCause() + "\t" + e.getMessage());
//            return null;
//        });
//        System.out.println(Thread.currentThread().getName() + "线程先去忙其他任务");
//        // 主线程不要立刻结束，否则，completableFuture默认使用线程池会立刻关闭
//        TimeUnit.SECONDS.sleep(3);

    }

    private static void future1() throws InterruptedException, ExecutionException {
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t ----come in");
            int result = ThreadLocalRandom.current().nextInt(10);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("----------->1秒钟后出结果:" + result);
            return result;
        });

        System.out.println(Thread.currentThread().getName() + "线程先去忙其他任务");
        System.out.println(completableFuture.get());
    }
}
